<?php
namespace app\process;

use Workerman\Timer;
use Workerman\Worker;
use support\Log;

class ThsProcessor
{
    public function onWorkerStart(Worker $worker)
    {
        // 每10秒执行一次
        Timer::add(2, function() {
            $this->processBatch();
        });
    }
    
    protected function processBatch()
    {
        // 批处理逻辑 
        Log::info("订单处理成功: 耗时：".date("Y-m-d H:i:s")); 
        $hour = date('H');//9;
        $minute = date('i'); //43;  
//         if(in_array(date('w'), [6,0])){
//             echo date('Y-m-d H:i:s')."---周末无需跑！\r\n";exit;
//         }
//         if($hour < 9 || $hour >= 15 ){
//             echo date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 9 && $minute < 25){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 11 && $minute > 30 ){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }
//         if($hour == 12){
//             echo  date('Y-m-d H:i:s')."---".$hour.':'.$minute.",时间段不允许\r\n";exit;
//         }  
        $url = "https://stockpage.10jqka.com.cn/spService/002552/Header/realDxjl"; 
        $html = self::curlGet($url);  
        
        preg_match_all('/<tr><td>(.*?)<\/td><td><a href="\/(.*?)\/">(.*?)<\/a><\/td><td class="(.*?)">(.*?)<\/td><\/tr>/', $html, $matches, PREG_SET_ORDER);
        
        $dataRegex = [];
        foreach ($matches as $match) {
            $dataRegex[] = [
                'time' => $match[1],
                'stock_code' => $match[2],
                'stock_name' => $match[3],
                'class' => $match[4],
                'operation' => $match[5],
                'type' => strpos($match[4], 'plus') !== false ? '买入' : '卖出'
            ];
        }
        
        // 输出结果
        echo "使用DOMDocument解析结果:\n";
        print_r($html);
        
        echo "\n使用正则表达式解析结果:\n";
        print_r($dataRegex);
        
        
        
        
    }
    
    /**
     * curlGet
     * @param unknown $url
     * @return mixed
     */
    public static function curlGet($url){
        $ch = curl_init();
        $header [] = "push2ex.eastmoney.com";
        $xforip = rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 ) . "." . rand ( 1, 254 );
        $header [] = "X-FORWARDED-FOR: $xforip";
        $header [] = "CLIENT-IP: $xforip";
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header );
        curl_setopt ( $ch, CURLOPT_ENCODING, "" );
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt ( $ch, CURLOPT_TIMEOUT, 3 ); // 设置1秒超时
        $output = curl_exec ( $ch );
        curl_close($ch);
        return $output;
    }
}